package cn.ow.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import cn.ow.bean.StockExcelEntity;
import cn.ow.converter.ConverterDataListener;
import cn.ow.dto.ExcelEnum;
import cn.ow.entity.song.Song;
import cn.ow.entity.stock.StockBlackEntity;
import cn.ow.entity.stock.StockEntity;
import cn.ow.mapper.song.SongMapper;
import cn.ow.mapper.stock.StockBlackMapper;
import cn.ow.mapper.stock.StockMapper;
import cn.ow.utils.SnowflakeIdGenerator;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.sql.Wrapper;
import java.util.Date;
import java.util.List;

/**
 * @author : owen
 * @email zhaoouyang163@gmail.com
 * @date : 2023/11/5
 */
@RestController
public class IndexController {

    @Autowired
    StockMapper stockMapper;

    @Autowired
    StockBlackMapper stockBlackMapper;



    @GetMapping("start")
    public String getIndex(String label) throws Exception{
        index(stockMapper,stockBlackMapper);
        System.out.println("导入完成");
        return "ok";
    }

    @GetMapping("blackdata")
    public String start_black(String label) throws Exception{
        blackData(stockMapper,stockBlackMapper);
        System.out.println("导入完成");
        return "ok";
    }


    private static void blackData(StockMapper stockMapper,StockBlackMapper stockBlackMapper) {
        // List<File> list=FileUtil.loopFiles("/Users/zhaoouyang/IdeaProjects/gupiao/gupiao/data");
        File file= FileUtil.file("/Users/zhaoouyang/IdeaProjects/gupiao/gupiao/black_data");
        // System.out.println(file.getName());
        File[] files=file.listFiles();
        for (File f1:files){
            if(FileUtil.isDirectory(f1)){

//                System.out.println(f1.getAbsolutePath());
//                System.out.println(f1.getName());
                List<File> listFile=FileUtil.loopFiles(f1);
                String f1Name=f1.getName();


                for (File f2:listFile){
                    // System.out.println(f2.getAbsolutePath());
                    // System.out.println(f2.getName());
                   /* if(f2.getName().indexOf("csv")!=-1){
                        // FileUtil.del(f2);
                        // convertCSVToXLSX(f2);
                        String path=f2.getAbsolutePath();
                        String name=f2.getName();
                        name=name.replace(".csv",".xlsx");
                        path=path.replace(f2.getName(),name);
                        System.out.println(path);
                        // System.out.println(f2.getPath());
                        // convertCSVToXLSX(f2.getAbsolutePath(),path);

                        //convertCSVToXLSXByEasyExcel(f2, path);
                        // StockExcelEntity

                    }else */
                    if(f2.getName().indexOf("xlsx")!=-1){
                        bianliExcelBlack(f1,f2,ExcelEnum.zidingyi,stockMapper,stockBlackMapper);
                    }
                }
            }
        }
    }


    private static void index(StockMapper stockMapper,StockBlackMapper stockBlackMapper) {
        // List<File> list=FileUtil.loopFiles("/Users/zhaoouyang/IdeaProjects/gupiao/gupiao/data");
        File file= FileUtil.file("/Users/zhaoouyang/IdeaProjects/gupiao/gupiao/data");
        // System.out.println(file.getName());
        File[] files=file.listFiles();
        for (File f1:files){
            if(FileUtil.isDirectory(f1)){
                if(f1.getName().equals("20240823")){
                    continue;
                }
//                System.out.println(f1.getAbsolutePath());
//                System.out.println(f1.getName());
                List<File> listFile=FileUtil.loopFiles(f1);
                String f1Name=f1.getName();
                stockMapper.delete(Wrappers.<StockEntity>lambdaQuery().eq(StockEntity::getStockTime,f1Name));

                for (File f2:listFile){
                    // System.out.println(f2.getAbsolutePath());
                    // System.out.println(f2.getName());
                   /* if(f2.getName().indexOf("csv")!=-1){
                        // FileUtil.del(f2);
                        // convertCSVToXLSX(f2);
                        String path=f2.getAbsolutePath();
                        String name=f2.getName();
                        name=name.replace(".csv",".xlsx");
                        path=path.replace(f2.getName(),name);
                        System.out.println(path);
                        // System.out.println(f2.getPath());
                        // convertCSVToXLSX(f2.getAbsolutePath(),path);

                        //convertCSVToXLSXByEasyExcel(f2, path);
                        // StockExcelEntity

                    }else */
                    if(f2.getName().indexOf("xlsx")!=-1){
                        //FileUtil.del(f2);
                        //ExcelReader excelReader=ExcelUtil.getReader(f2);
                        if(f2.getName().indexOf(ExcelEnum.zhangfu.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.zhangfu,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.wufenzhangfu.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.wufenzhangfu,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.liushirizhangfu.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.liushirizhangfu,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.buyliang.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.buyliang,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.sellliang.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.sellliang,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.chengjiaoliang.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.chengjiaoliang,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.chengjiaoe.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.chengjiaoe,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.guxilvttm.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.guxilvttm,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.huanshoulv.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.huanshoulv,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.zhengfu.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.zhengfu,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.shiyinglvttm.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.shiyinglvttm,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.shiyinglvstatic.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.shiyinglvstatic,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.shijinglv.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.shijinglv,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.liangbi.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.liangbi,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.weibi.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.weibi,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.zongshizhi.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.zongshizhi,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.liutongzhi.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.liutongzhi,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.zongguben.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.zongguben,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.wurizhangfu.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.wurizhangfu,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.shirizhangfu.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.shirizhangfu,stockMapper,stockBlackMapper);
                        }else if(f2.getName().indexOf(ExcelEnum.ershirizhangfu.getValue())!=-1){
                            bianliExcel(f1,f2,ExcelEnum.ershirizhangfu,stockMapper,stockBlackMapper);
                        }
                    }
                }
            }
        }
    }

    private static void bianliExcel(File f1,File f2,ExcelEnum excelEnum,StockMapper stockMapper,StockBlackMapper stockBlackMapper) {
        bianliExcel1(f1,f2,excelEnum,stockMapper,stockBlackMapper);
    }


    private static void bianliExcelBlack(File f1,File f2,ExcelEnum excelEnum,StockMapper stockMapper,StockBlackMapper stockBlackMapper) {
        ConverterDataListener data=new ConverterDataListener();
        EasyExcel.read(f2, StockExcelEntity.class, data).excelType(ExcelTypeEnum.XLSX).sheet().doRead();
        List<StockExcelEntity> lists=data.list;
        for (StockExcelEntity entity:lists){
            // 校验黑名单
            StockEntity stock1=new StockEntity();
            getStockEntityData(f1, excelEnum, entity, stock1);
            insertBlackStock(stockBlackMapper, stock1);
        }
    }


    private static void bianliExcel1(File f1,File f2,ExcelEnum excelEnum,StockMapper stockMapper,StockBlackMapper stockBlackMapper) {
        ConverterDataListener data=new ConverterDataListener();
        EasyExcel.read(f2, StockExcelEntity.class, data).excelType(ExcelTypeEnum.XLSX).sheet().doRead();
        List<StockExcelEntity> lists=data.list;
        for (StockExcelEntity entity:lists){
            // 校验黑名单
            StockBlackEntity blackEntity=stockBlackMapper.selectOne(Wrappers.<StockBlackEntity>lambdaQuery().eq(StockBlackEntity::getCode,entity.getCode()));
            if(ObjectUtil.isEmpty(blackEntity)){
                StockEntity stock=new StockEntity();
                getStockEntityData(f1, excelEnum, entity, stock);
                String preCode=stock.getCode().substring(0,3);
                String preCode1=stock.getCode().substring(0,1);
                if("8".equals(preCode1)||"3".equals(preCode1)
                        ||"688".equals(preCode)
                        ||"871".equals(preCode)
                        ||"300".equals(preCode)
                        ||"301".equals(preCode)
                        ||stock.getName().indexOf("ST")!=-1){

                    insertBlackStock(stockBlackMapper, stock);
                }else{
                    stockMapper.insert(stock);
                }
            }else{
                if(ObjectUtil.isNotEmpty(entity.getCode())){
                    stockMapper.delete(Wrappers.<StockEntity>lambdaQuery().eq(StockEntity::getCode,entity.getCode()));
                }
            }
        }
    }


    private static void bianliExcel2(File f1,File f2,ExcelEnum excelEnum,StockMapper stockMapper,StockBlackMapper stockBlackMapper) {
        ConverterDataListener data=new ConverterDataListener();
        EasyExcel.read(f2, StockExcelEntity.class, data).excelType(ExcelTypeEnum.XLSX).sheet().doRead();
        List<StockExcelEntity> lists=data.list;
        for (StockExcelEntity entity:lists){
            // 校验黑名单
            StockBlackEntity blackEntity=stockBlackMapper.selectOne(Wrappers.<StockBlackEntity>lambdaQuery().eq(StockBlackEntity::getCode,entity.getCode()));
            if(ObjectUtil.isEmpty(blackEntity)){
                StockEntity stock=new StockEntity();
                getStockEntityData(f1, excelEnum, entity, stock);
                stockMapper.insert(stock);
            }else{
                if(ObjectUtil.isNotEmpty(entity.getCode())){
                    stockMapper.delete(Wrappers.<StockEntity>lambdaQuery().eq(StockEntity::getCode,entity.getCode()));
                }
            }
        }
    }

    private static void insertBlackStock(StockBlackMapper stockBlackMapper, StockEntity stock1) {
        StockBlackEntity stockBlackEntity=new StockBlackEntity();
        BeanUtils.copyProperties(stock1,stockBlackEntity);
        stockBlackMapper.insert(stockBlackEntity);
    }


    private static void getStockEntityData(File f1, ExcelEnum excelEnum, StockExcelEntity entity, StockEntity stock) {
        BeanUtils.copyProperties(entity, stock);
        SnowflakeIdGenerator idGenerator=new SnowflakeIdGenerator(1);
        Long id=idGenerator.nextId();
        stock.setId(id);
        stock.setCreateTime(DateUtil.date());
        stock.setStatus(0L);
        stock.setType(excelEnum.getType());
        stock.setStockTime(Long.valueOf(f1.getName()));

        String code=stock.getCode();
        if(code.length()==3){
            code="000"+code;
            stock.setCode(code);
        }else if(code.length()==4){
            code="00"+code;
            stock.setCode(code);
        }else if(code.length()==2){
            code="0000"+code;
            stock.setCode(code);
        }else if(code.length()==1){
            code="00000"+code;
            stock.setCode(code);
        }else if(code.length()==5){
            code="0"+code;
            stock.setCode(code);
        }
    }

}
